Methods in Java

Q: What is method?
Ans: Its a block OR piece of code which can be reused.


Difference between Constructor and Methods:

       Constructor					Methods
1. names are predefined	      		1. names are user defined

2. Will not return any value        2. It may OR may not return the value  

3. Multiple objects are reqd        3. Using one object of the class we can   
to run multiple constructor				run multiple methods

4. Different tasks with same       4. It is possible by 
    set of arguments is not             changing method names
    possible

5. Here we have default            5. No default method 
 constructor concept                     concept

6. If you create object to the     6. If we create object to class the method  
 class the respective constructor     will not get execute. We must call the  
 will get execute                       method to execute it.
 
7. constructor overriding          7. Method overloading & method
   is not possible. bcoz	        overriding is possible
   the constructor name &
   class name must be same                       
-----------------------------------------------

Method of types:
1. The method which doesnot return value
   (a) without arguments
   (b) with arguments

2. The Methods which returns the value
   (a) without arguments
   (b) with arguments
------------------------------

Method Overloading:
 - It is a process of writing multiple methods with same name & having different signatures.
 - Method overloading takes place in the same (independent) class OR in the inheritance (Parent & Child class)
 - In method overloading return type is not considered where as only method name & signature (arguments) is considered.


Rules for Method overloading:
1. The no. of arguments used in the methods should be different

2. The datatype of the arguments used in the methods should be different.

3. The Sequence of datatype of the arguments used in the methods should be different
   
----------------------------------

Q: Can a method return multiple times?
Ans: No. Method can return only once.


Q: Can a method return multiple values?
Ans: Yes. A method can return single or multiple values
Ex: Array OR Collection OR Object are the best example through which you can return multiple values


Q: What a method can return?
Ans: A Method in java can return ANY type of data.
EX: All datatypes, class, interface, abstract class, object, collection, array etc etc etc etc


Q: What a method can accept as argument?
Ans: A Method in java can accept ANY type of data as argument.
EX: All datatypes, class, abstract class, interface, object, collection, array etc etc etc etc


Q: Can we write nested methods in java?
Ans: No. But we can call one method inside another method.


Q: Can we call the method in its own body?
Ans: Yes. It is k.a., Recursion. (calling the method in its own body is k.a, Recursion)


Q1: WAM to sort array?
Q2: WAM to reverse array?
Q3: WAM to find missing number?
Q4: WAM to add array?
Q5: WAM where the output of method1() is a input to method2()?
---------------------------------------
By Value and By Reference:

By value: It takes place in methods at variable level. Any modification done to the variable inside the method will not reflect in the out side method.


By Reference:It takes place in methods at Object level. Any modification done to the object inside the method will reflect in the out side method.

============================
Class members
1. instance members
2. static members
===========================
Debug in Eclipse tool
============================
this and super keyword
===========================
Nested class in Java
===========================
